package ro.imc.rest.server.util;

import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import de.imc.clixrestdto.provider.JAXBContextResolver;


/**
 * Created by IntelliJ IDEA.
 * User: Blickle
 * Date: 27.10.12
 * Helper class to provide access to CLIX Rest API
 */
public class Connection {

    private Client client;
    private WebResource webResource;

    public Connection(java.net.URI Uri, String user, String password) {
         client = createClient();
         webResource = client.resource(Uri);
         client.addFilter(new HTTPBasicAuthFilter(user, password));
       //  client.addFilter(new UserAgentFilter());
    }

    public WebResource getWebResource() {
        return webResource;
    }

    public static Client createClient() {
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getClasses().add(JAXBContextResolver.class);
        Client client = Client.create(clientConfig);
        return client;
    }

    /**
     *
     * @param itemFullURL: URL including IP, Port, and full path
     * @param user
     * @param password
     * @return
     */
    public static WebResource getResourceFromFullURL(String itemFullURL, String user, String password)  {
       Client client = Connection.createClient();
       client.addFilter(new HTTPBasicAuthFilter(user, password));
       return client.resource(itemFullURL);

    }

}
